package com.golang.api.util;

import com.fasterxml.jackson.annotation.JsonIgnore;

import java.io.Serializable;
import java.util.Objects;

/**
 * 统一响应
 *
 * @author: 万明宇 (warApe)
 **/
public class Result<T> implements Serializable {

  private static final long serialVersionUID = -1;

  private Integer code;
  private String message;
  private T result;

  @JsonIgnore
  public boolean isSuccess () {
    return CommonRespCode.SUCCESS.equalsCode(this.code);
  }

  public Result () {
  }

  public Result (Integer code, String message) {
    this.code = code;
    this.message = message;
  }

  public Result (Integer code, String message, T result) {
    this.code = code;
    this.message = message;
    this.result = result;
  }

  public Result (StatusEnumSupport statusEnumSupport) {
    this.code = statusEnumSupport.getCode();
    this.message = statusEnumSupport.getMessage();
  }

  public Result (StatusEnumSupport statusEnumSupport, T result) {
    this.code = statusEnumSupport.getCode();
    this.message = statusEnumSupport.getMessage();
    this.result = result;
  }

  public Integer getCode () {
    return code;
  }

  public Result setCode (Integer code) {
    this.code = code;
    return this;
  }

  public String getMessage () {
    return message;
  }

  public Result setMessage (String message) {
    this.message = message;
    return this;
  }

  public T getResult () {
    return result;
  }

  public Result setResult (T result) {
    this.result = result;
    return this;
  }

  @Override
  public boolean equals (Object o) {
    if (this == o) {
      return true;
    }
    if (o == null || getClass() != o.getClass()) {
      return false;
    }
    Result<?> result1 = (Result<?>) o;
    return code.equals(result1.code) && result.equals(result1.result);
  }

  @Override
  public int hashCode () {
    return Objects.hash(code, result);
  }
}
